package edu.dhbw.andar.sample;

import java.util.ArrayList;
import java.util.HashMap;

import org.cocos2d.nodes.CCNode;
import org.cocos2d.nodes.CCSprite;
import org.cocos2d.nodes.CCSpriteFrame;
import org.cocos2d.nodes.CCSpriteSheet;
import org.cocos2d.types.CGPoint;
import org.cocos2d.types.CGRect;

/**Interface for performing actions with the player's character*/
public class ActionSlots {
	//Fields
	private CCNode _spriteNode;
	private ArrayList<CCSprite> _slots = new ArrayList<CCSprite>();
	private HashMap<String, CCSpriteFrame> _contentFrames = new HashMap<String, CCSpriteFrame>();
	private int _currentSlot, _touchID;
	private CGPoint _touchPoint, _startTouchPoint;
	private boolean _isTouched;
	private float _goalAngle, _currentAngle;
	
	/**Constructor initializes graphics, position and touch*/
	public ActionSlots(String imageFile, float startX, float startY){
		_spriteNode = CCNode.node();
		_spriteNode.setPosition(CGPoint.ccp(startX, startY));
		_isTouched = false;
		_currentSlot = 0;
		_touchID = -1;
		_goalAngle = (float)Math.toRadians(270);
		_currentAngle = (float)Math.toRadians(270);
		//Prepare action content icons
		CCSpriteSheet actionIcons = CCSpriteSheet.spriteSheet("action_icons_new.png");
		_contentFrames.put("knife",CCSpriteFrame.frame(
				actionIcons.getTexture(), CGRect.make(0, 0, 100, 100), CGPoint.ccp(88,  88)));
		_contentFrames.put("pistol", CCSpriteFrame.frame(
				actionIcons.getTexture(), CGRect.make(100, 0, 100, 100), CGPoint.ccp(88,  88)));
		_contentFrames.put("firstAidKit", CCSpriteFrame.frame(
				actionIcons.getTexture(), CGRect.make(200, 0, 100, 100), CGPoint.ccp(88, 88)));
		//Add three action slots
		for(int i = 0; i < 3; i++){
			//Create sprites for slot and icon
			CCSprite actionSlot = CCSprite.sprite("activeSlot.png");
			CCSprite slotContent = new CCSprite();
			if(i == 0) slotContent.setDisplayFrame(_contentFrames.get("knife"));
			if(i == 1) slotContent.setDisplayFrame(_contentFrames.get("pistol"));
			if(i == 2) slotContent.setDisplayFrame(_contentFrames.get("firstAidKit"));
			//Add content to slot and then slot to parent and array
			actionSlot.addChild(slotContent);
			_spriteNode.addChild(actionSlot);
			_slots.add(actionSlot);
		}
		_spriteNode.setVisible(false);
	}
	
	/**Action slots' behavior*/
	public void update(float dt){
		//Put current slot in front
		_spriteNode.reorderChild(_slots.get(_currentSlot), 0);
		//Correct angles according to touch
		if(_isTouched){
			float dist = _startTouchPoint.x - _touchPoint.x;
			_currentAngle = _goalAngle - dist / 150 * (float)Math.PI * 0.3f;
		}
		//Correct angles according to which one is the current slot
		else{
			if(_currentAngle < _goalAngle){
				_currentAngle += Math.toRadians(360 * dt);
				_currentAngle = Math.min(_currentAngle, _goalAngle);
			} else if(_currentAngle > _goalAngle){
				_currentAngle -= Math.toRadians(360 * dt);
				_currentAngle = Math.max(_currentAngle, _goalAngle);
			}
		}
		//Set scale of a slot in relation to its angle
		for(int i = 0; i < _slots.size(); i++){
			CCSprite sprite = _slots.get(i);
			sprite.setPosition(
					(float)Math.cos(_currentAngle + i * (Math.PI * 2 / 3)) * 88,
					(float)Math.sin(_currentAngle + i * (Math.PI * 2 / 3)) * 32);
			sprite.setScale((float)Math.cos(_currentAngle + i * (Math.PI * 0.66) + Math.PI * 0.5) * 0.3f + 0.7f);
		}
	}
	
	////GETTERS, SETTERS AND HELP METHODS
	/** */
	public void updateTouch(CGPoint touchPoint){
		_touchPoint = touchPoint;
	}
	
	/** */
	public void swapActions(){
		float dist = _startTouchPoint.x - _touchPoint.x;
		//
		if(dist < 0){
			_goalAngle += Math.toRadians(120);
			_currentSlot = (_currentSlot + 2) % 3;
		}
		//
		else{
			_goalAngle -= Math.toRadians(120);
			_currentSlot = (_currentSlot + 1) % 3;
		}
		System.out.println("swap action");
	}
	
	/** */
	public boolean isIntersecting(CGRect otherRect){
		CGRect boundingBox = CGRect.make(
				_spriteNode.getPosition().x - 96,
				_spriteNode.getPosition().y - 66,
				200,
				150);
		return CGRect.intersects(boundingBox, otherRect);
	}
	
	/** */
	public boolean hasTouchID(int touchID){
		return _touchID == touchID;
	}
	
	/** */
	public boolean wasSwiped(){
		if(_startTouchPoint == null){
			return false;
		}
		float dist = (float)Math.hypot(
				_startTouchPoint.x - _touchPoint.x,
				_startTouchPoint.y - _touchPoint.y);
		System.out.println("was swiped");
		return dist > 55;
	}
	
	/** */
	public void startTouch(int touchID, CGPoint touchStart){
		_isTouched = true;
		_touchID = touchID;
		_touchPoint = touchStart;
		_startTouchPoint = touchStart;
	}
	
	/** */
	public void resetTouch(){
		_isTouched = false;
		_touchID = -1;
		_touchPoint = null;
		_startTouchPoint = null;
	}
	
	/** */
	public void setVisible(boolean visible){
		_spriteNode.setVisible(visible);
	}
	
	/** */
	public void setContent(int contentIndex, String content){
		_slots.get(contentIndex).setDisplayFrame(_contentFrames.get(content));
	}
	
	/** */
	public String getContent(int contentIndex){
		//TODO: make it possible to get content for a given index
		return "no idea";
	}
	
	/** */
	public String getCurrentAction(){
		if(_currentSlot == 0){
			return "bench";
		} else if(_currentSlot == 1){
			return "superman";
		} else{
			return "sofa";
		}
	}
	
	/** */
	public CCNode getNode(){
		return _spriteNode;
	}
}
